package otherapi;

import java.util.Optional;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author LiDong
 * @version 1.0.0
 * @createTime 7/15/2022 9:54 AM
 */
public class JacksonTest {

    private static final Logger logger = LoggerFactory.getLogger(JacksonTest.class);

    @Test
    public void test1() {
        JsonNode jsonNode = new ObjectNode(JsonNodeFactory.instance);
        JsonNode test = jsonNode.findValue("test");
        logger.info(String.valueOf(test));
    }

    @Test
    public void test2() {
        JsonNode jsonNode = new ObjectNode(JsonNodeFactory.instance);
        JsonNode testNode1 = Optional.ofNullable(jsonNode.findValue("test1")).orElse(new ObjectNode(JsonNodeFactory.instance));
        JsonNode testNode2 = Optional.ofNullable(testNode1.findValue("test2")).orElse(new ObjectNode(JsonNodeFactory.instance));
        for (JsonNode node : testNode2) {
            logger.info(String.valueOf(node));
        }
        logger.info("ok");
    }

    @Test
    public void test3() {
        ObjectMapper mapper = new ObjectMapper();
        ObjectNode node = mapper.createObjectNode();
        node.put("name", "test");
        node.put("age", 20);
        logger.info("{}", node);

        ArrayNode array = mapper.createArrayNode();
        array.add(node);
        logger.info("{}", array);
    }

}
